
import pandas as pd
import akshare as ak
import time

# 设置Pandas的显示选项
pd.set_option('display.max_rows', None)  # 显示所有行
pd.set_option('display.max_columns', 500)  # 设置最大列数为500，你可以根据需要调整这个值

secu_list_df = pd.read_csv("secu_list.csv")
secu_list_df['truncated_secu_code'] = secu_list_df['secu_code'].str[:-3]

start_time = time.time()
stock_zh_a_spot_em_df = ak.stock_zh_a_spot_em()

# 步骤 1: 去除 secu_code 的最后三个字符

# 步骤 2: 使用处理后的 secu_code 与 stock_zh_a_spot_em_df 进行内连接
joined_df = pd.merge(secu_list_df, stock_zh_a_spot_em_df,
                     left_on='truncated_secu_code',
                     right_on='代码',
                     how='inner')

# 如果不再需要 'truncated_secu_code' 列，可以将其删除
joined_df.drop(columns=['truncated_secu_code'], inplace=True)

# 计算 high_return 和 open_return
joined_df['high_return'] = (joined_df['最高'] - joined_df['昨收']) / joined_df['昨收'] * 100
joined_df['open_return'] = (joined_df['今开'] - joined_df['昨收']) / joined_df['昨收'] * 100

# 根据 open_return 对 DataFrame 进行排序
joined_df_sorted = joined_df.sort_values(by='open_return', ascending=False)  # ascending=False 表示降序排序

print(stock_zh_a_spot_em_df)
end_time = time.time()
execution_time = end_time - start_time
print(f"程序运行时间为：{execution_time} 秒")
stock_individual_fund_flow_rank_df = ak.stock_individual_fund_flow_rank(indicator="今日")


stock_individual_fund_flow_rank_df['fund_flow'] = pd.to_numeric(
    stock_individual_fund_flow_rank_df['今日主力净流入-净占比'],
    errors='coerce'  # 无法转换的值将被设置为NaN
)

stock_individual_fund_flow_rank_df['change_pct'] = pd.to_numeric(
    stock_individual_fund_flow_rank_df['今日涨跌幅'],
    errors='coerce'  # 无法转换的值将被设置为NaN
)

stock_individual_fund_flow_rank_df = (
    stock_individual_fund_flow_rank_df
    .assign(**{
        'fund_flow': pd.to_numeric(stock_individual_fund_flow_rank_df['今日主力净流入-净占比'] , errors='coerce'),
        'change_pct': pd.to_numeric(stock_individual_fund_flow_rank_df['今日涨跌幅'] , errors='coerce')
    })
    .dropna(subset=['fund_flow', 'change_pct'])
    .query("fund_flow >= 4 and change_pct > 3")
)

# stock_individual_fund_flow_rank_df = stock_individual_fund_flow_rank_df[(stock_individual_fund_flow_rank_df["fund_flow"]>=8 )& (stock_individual_fund_flow_rank_df["change_pct"] > 5)]
print(stock_individual_fund_flow_rank_df)


# 步骤 2: 使用处理后的 secu_code 与 stock_zh_a_spot_em_df 进行内连接
joined_dfxx = pd.merge(stock_individual_fund_flow_rank_df, joined_df_sorted,
                     left_on='代码',
                     right_on='代码',
                     how='inner')

# 使用 loc 方法来筛选和打印满足条件的行
# 对于总市值在 (1500000000, 150000000000] 范围内的行，如果 change_pct > 5，则打印这些行
mask1 = (joined_dfxx['总市值'] > 1500000000) & (joined_dfxx['总市值'] <= 150000000000) & (joined_dfxx['change_pct'] > 5) & (joined_dfxx['换手率'] > 1) & (joined_dfxx['换手率'] < 20)
print(joined_dfxx.loc[mask1,['代码', '名称_x', '最新价_x', 'change_pct','fund_flow', '量比', '换手率', 'open_return']].sort_values(by='open_return', ascending=False))

# joined_df_sorted = joined_df.sort_values(by='open_return', ascending=False)  # ascending=False 表示降序排序

# 对于总市值大于或等于 150000000000 的行，如果 change_pct > 3，则打印这些行
mask2 = (joined_dfxx['总市值'] >= 150000000000) & (joined_dfxx['总市值'] > 3) & (joined_dfxx['换手率'] < 20)
print(joined_dfxx.loc[mask2])
x=1
#
# Index(['序号_x', '代码', '名称_x', '最新价_x', '今日涨跌幅', '今日主力净流入-净额', '今日主力净流入-净占比',
#        '今日超大单净流入-净额', '今日超大单净流入-净占比', '今日大单净流入-净额', '今日大单净流入-净占比',
#        '今日中单净流入-净额', '今日中单净流入-净占比', '今日小单净流入-净额', '今日小单净流入-净占比', 'fund_flow',
#        'change_pct', 'Unnamed: 0', 'secu_code', '序号_y', '名称_y', '最新价_y', '涨跌幅',
#        '涨跌额', '成交量', '成交额', '振幅', '最高', '最低', '今开', '昨收', '量比', '换手率',
#        '市盈率-动态', '市净率', '总市值', '流通市值', '涨速', '5分钟涨跌', '60日涨跌幅', '年初至今涨跌幅',
#        'high_return', 'open_return'],
#       dtype='object')